package org.wyn.mongodb.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.bson.types.ObjectId;
import org.wyn.mongodb.bean.Album;
import org.wyn.mongodb.bean.Picture;
import org.wyn.mongodb.bean.User;
import org.wyn.mongodb.common.SearchBDProvider;

import com.google.code.morphia.dao.BasicDAO;
/**
 * 相册mongodb dao实现层
 * @author wyn
 *
 */
public class AlbumDao extends BasicDAO<Album,ObjectId>{
	/**普通类型相册*/
	public static final String COMMON = "common";
	/**系统创建的头像相册*/
	public static final String PORTRAIT = "portrait";
	public AlbumDao(){
		super(SearchBDProvider.getInstance());
	}
	/**
	 *创建相册
	 * @param userName 创建者userName
	 * @param albumName 相册名
	 * @return
	 */
	public boolean createAlbum(String userName,String albumName){
		Album a = new Album();
		a.setUserName(userName);
		a.setAlbumName(albumName);
		a.setAlbumType(AlbumDao.COMMON);
		a.setCreateTime(System.currentTimeMillis());
		a.setVisitCount(0);
		save(a);
		ensureIndexes();
		return true;
	}
	/**
	 * 创建头像相册
	 * @param userName 创建者
	 * @return
	 */
	public boolean createPortraitAlbum(String userName){
		Album album = findOne(createQuery().filter("userName", userName).filter("albumType", AlbumDao.PORTRAIT));
		if(null != album)//已经有头像相册了,防止重复创建
			return false;
		album = new Album();
		album.setUserName(userName);
		album.setAlbumName("头像相册");
		album.setAlbumType(AlbumDao.PORTRAIT);
		album.setDescription("系统创建的头像相册");
		album.setCreateTime(System.currentTimeMillis());
		album.setVisitCount(0);
		save(album);
		ensureIndexes();
		return true;
	}
	/**
	 * 向头像相册中添加一张头像
	 * @param userName 用户账号
	 * @param fileId 头像图片Id
	 * @return
	 */
	public boolean addImageToPortrait(String userName,ObjectId fileId){
		//找到该用户的头像相册
		Album album = findOne(createQuery().filter("userName", userName).filter("albumType", AlbumDao.PORTRAIT));
		if(null == album){
			return false;
		}
		//相册中添加一张图片
		List<Picture> pics = album.getPictures();
		if(null == pics)
			pics = new ArrayList<Picture>();
		Picture p = new Picture();
		p.setFileId(fileId);
		p.setFileName("");
		p.setCreateTime(System.currentTimeMillis());
		p.setVisitCount(0);
		pics.add(p);
		save(album);
		ensureIndexes();
		return true;
	}
	/**
	 * 根据相册id得到相册名
	 * @param albumId
	 * @return 相册不存在返回null
	 */
	public String getAlbumName(ObjectId albumId){
		Album a = findOne("id", albumId);
		if(null == a)
			return null;
		else
			return a.getAlbumName();
	}
	/**
	 * 根据相册id得到相册拥有者userName
	 * @param albumId
	 * @return 相册不存在返回null
	 */
	public String getUserName(ObjectId albumId){
		Album a = findOne("id", albumId);
		if(null == a)
			return null;
		else
			return a.getUserName();
	}
	public static void main(String[] args) {
		AlbumDao aDao = new AlbumDao();
		UserDao uDao = new UserDao();
		//为每个存在的用户创建一个头像相册
		for(User user : uDao.createQuery()){
			String userName = user.getUserName();
			Album album = aDao.createQuery().filter("userName", userName).filter("albumType", AlbumDao.PORTRAIT).get();
			if(null == album){
				album = new Album();
				album.setUserName(userName);
				album.setAlbumName("头像相册");
				album.setAlbumType(AlbumDao.PORTRAIT);
				album.setDescription("系统创建的头像相册");
				album.setCreateTime(System.currentTimeMillis());
				album.setVisitCount(0);
				aDao.save(album);
				aDao.ensureIndexes();
			}
		}
		//把用户创建的相册类型置为common
		for(Album album : aDao.createQuery()){
			if(null == album.getAlbumType()){
				album.setAlbumType(AlbumDao.COMMON);
				aDao.save(album);
				aDao.ensureIndexes();
			}
		}
	}
}
